﻿using BBICMS.Model;
using BBICMS.Repository;
using BBICMS.WebUI.Filters;
using BBICMS.WebUI.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
     [SIGAuth]
    
    public class CategoryController : Controller
    {
        private ApplicationUnit _unit = new ApplicationUnit();
        //
        // GET: /AdminCategories/
       
        public ActionResult Index()
        {
            CategoriesListViewModel vm = new CategoriesListViewModel();

            var lCategories = _unit.Categories.GetAll();

            if (lCategories != null)
            {
                vm.Categories = lCategories.ToList();
                return View(vm);
            }


            return View("NotFound");

        }


       
        public ActionResult Create()
        {
            return View("Edit", new Category());
        }

        public ViewResult Edit(int id)
        {
            Category vCategory = _unit.Categories.GetById(id);
            return View(vCategory);
        }

       
        [HttpPost]
        public ActionResult Edit(Category vCategory)
        {

            //if (vCategory.CountryID == 0)
            //{
            //    vCategory.AddedBy = User.Identity.Name;
            //    vCategory.AddedDate = DateTime.Now;

            //}
            //else
            //{
            //    vCountry.UpdatedBy = User.Identity.Name;
            //    vCountry.UpdatedDate = DateTime.Now;
            //}
            if (ModelState.IsValid)
            {
                if (vCategory.Id == 0)
                    _unit.Categories.Add(vCategory);
                else
                    _unit.Categories.Update(vCategory);

                this._unit.SaveChanges();

                TempData["message"] = string.Format("{0} 已经保存", vCategory.Title);
                return RedirectToAction("Index");
            }
            else
            {
                return View(vCategory);
            }


        }


        protected override void Dispose(bool disposing)
        {
            this._unit.Dispose();
            base.Dispose(disposing);
        }

    }
}
